package org.example.design.One;

import lombok.SneakyThrows;

import java.lang.reflect.Constructor;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @Auther:Liu
 * @Testname:App
 * @Date:2025/8/15 9:37
 */
public class App {

    public static void a() {
        DemoA a = DemoA.getInstance();
        DemoA b = DemoA.getInstance();
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println("(a==b) = " + (a == b));
    }

    public static void b1() {
        DemoB b = DemoB.getInstance();
        DemoB c = DemoB.getInstance();
        System.out.println("b = " + b);
        System.out.println("c = " + c);
        System.out.println("(b==c) = " + (b == c));
    }

    public static void b2() {
        DemoC b = DemoC.getInstance();
        DemoC c = DemoC.getInstance();
        System.out.println("b = " + b);
        System.out.println("c = " + c);
        System.out.println("(b==c) = " + (b == c));
    }

    @SneakyThrows
    public static void c() {
        AtomicReference<Object> b1 = new AtomicReference<>(), b2 = new AtomicReference<>();
        Thread t1 = new Thread(() -> {
            b1.set(DemoB.getInstance());
        });
        Thread t2 = new Thread(() -> {
            b2.set(DemoB.getInstance());
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("b1 = " + b1.get());
        System.out.println("b2 = " + b2.get());
        System.out.println("b1.get()==b2.get() = " + b1.get() == b2.get());
    }

    @SneakyThrows
    public static void d(){
        Class<?> aClass = Class.forName("org.example.design.One.DemoD");
        Constructor<?> constructor = aClass.getDeclaredConstructor();
        constructor.setAccessible(true);
        DemoD d = (DemoD) constructor.newInstance();
        DemoD e = (DemoD) constructor.newInstance();
        System.out.println("d = " + d);
        System.out.println("e = " + e);
        System.out.println("d==e = " + (d==e));
    }


    public static void e(){
        DemoE e = DemoE.INSTANCE;
        System.out.println("e = " + e);
        DemoE e1 = DemoE.INSTANCE;
        System.out.println("e1 = " + e1);
        System.out.println("(e==e1) = " + (e == e1));
        e.a();
        e.b();
        e1.a();
        e1.b();
    }

    public static void main(String[] args) {
        c();
    }
}
